﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Client;
using System.Configuration;
using Microsoft.Xrm.Client.Services;

namespace CRM2011.FetchXMLExecuteTool
{
    public partial class Main : Form
    {
        private IOrganizationService service;
        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            ddlDeploymentType.SelectedIndex = 0;
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            ShowWaitingBox waiting = null;
            try
            {
                bool isValue = CheckIfUserDataValid();
                if (isValue)
                //if (true)
                {
                    waiting = new ShowWaitingBox("Connecting", "Connecting to CRM.Please Wait....");
                    waiting.Start();
                    string connectionString = string.Empty;
                    var serverAddress=txtServerAddress.Text;
                    var domain=txtDomain.Text;
                    var userName=txtUserName.Text;
                    var passWord=txtPassword.Text;
                    var deviceId = txtDeviceID.Text;
                    var devicePassword = txtDevicePassword.Text;
                    switch (ddlDeploymentType.SelectedItem.ToString())
                    {
                        case "ActiveDirectory": connectionString = "Url="+serverAddress+"; Domain="+domain+"; Username="+userName+"; Password="+passWord; break;
                        case "Claims/IFD": connectionString = "Url=" + serverAddress + "; Username=" + userName + "; Password=" + passWord; break;
                        case "CRM Online": connectionString = "Url="+serverAddress+"; Username="+userName+"; Password="+passWord+"; DeviceID="+deviceId+"; DevicePassword="+DevicePassword; break;
                        default: connectionString = "Url=" + serverAddress + "; Domain=" + domain + "; Username=" + userName + "; Password=" + passWord; break;
                    }
                    var settings = new ConnectionStringSettings("Xrm", connectionString);
                    var connection = new CrmConnection(settings);
                    service= new OrganizationService(connection);
                    waiting.Stop();
                    FetchXMLForm fetchForm = new FetchXMLForm(service);
                    fetchForm.FormClosed += new FormClosedEventHandler(fetchForm_FormClosed);
                    //showing the main form
                    fetchForm.Show();
                    //hiding the current form
                    this.Hide();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                if (waiting != null)
                    waiting.Stop();
            }

        }

        private bool CheckIfUserDataValid()
        {
            string errorText = string.Empty;
          
            if (txtServerAddress.Text == "" || txtServerAddress.Text.Length == 0)
            {
                errorText += "Invalid Server Address\n";
            }
           
            if (txtUserName.Text == "" || txtUserName.Text.Length == 0)
            {
                errorText += "Invalid User Name\n";
            }
            if (txtPassword.Text == "" || txtPassword.Text.Length == 0)
            {
                errorText += "Invalid Password\n";
            }

            if ((txtDomain.Text == "" || txtDomain.Text.Length == 0))
            {
                errorText += "Invalid Domain\n";
            }





            if (errorText == string.Empty)
            {
                return true;
            }
            else
            {
                MessageBox.Show(errorText);
                return false;
            }

        }
        void fetchForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            // here you can do anything

            // we will close the application
            Application.Exit();
        }

        private void ddlDeploymentType_SelectedIndexChanged(object sender, EventArgs e)
        {
            var deploymentType = ddlDeploymentType.SelectedItem.ToString();
            if (deploymentType == "CRM Online")
            {
                label16.Visible = true;
                label14.Visible = true;
                txtDeviceID.Visible = true;
                DevicePassword.Visible = true;
                label7.Visible = true;
                txtDevicePassword.Visible = true;
                txtDomain.Visible = false;
                label6.Visible = false;
                label13.Visible = false;
                label9.Visible = true;
            }
            else if (deploymentType == "ActiveDirectory")
            {
                label16.Visible = false;
                label14.Visible = false;
                txtDeviceID.Visible = false;
                DevicePassword.Visible = false;
                label7.Visible = false;
                txtDevicePassword.Visible = false;
                txtDomain.Visible = true;
                label6.Visible = true;
                label13.Visible = true;
                label9.Visible = false;

            }
            else
            {
                label16.Visible = false;
                label14.Visible = false;
                txtDeviceID.Visible = false;
                DevicePassword.Visible = false;
                label7.Visible = false;
                txtDevicePassword.Visible = false;
                txtDomain.Visible = false;
                label6.Visible = false;
                label13.Visible = false;
                label9.Visible = false;
            }

        }








    }
}
